﻿<?php 
class AfterworkController extends Controller
{	
	public function ActionThisWeek($request)
	{
		$this->data['afterworks'] = AfterworkModel::GetWeek();		
		$this->View('Empty.php');
	}
	
	public function ActionNextWeek($request)
	{
		$this->data['afterworks'] = AfterworkModel::GetNextWeek();
		$this->View('Empty.php');
	}
	
	public function ActionDetail($request)
	{
		if (isset($request['id']))
		{
			$this->data['afterwork'] = $afterwork = AfterworkModel::Get($request['id']);
			$this->data['subscribers_yes'] = $afterwork->GetAnsweredSubscribers('Yes');
			$this->data['subscribers'] = $afterwork->GetAnsweredSubscribers();
		}	
		$this->View('Empty.php');
	}
	
	public function ActionAdd($request)
	{
		$login = LoginModel::GetFromUsername($_SESSION['login']);
		if (count($request) > 0)
		{
			$afterwork = new AfterworkModel();
			foreach ($request as $key => $value) {
				if ($key == 'date')
				{
					$date = str_replace('/', '-', $value);
					$afterwork->$key = date('Y-m-d', strtotime($date));
				}
				else
					$afterwork->$key = $value;
			}
			
			$afterwork->id_login = $login->id;
			$afterwork->selected = 0;
			$afterwork->save();
			header('Location: /Afterwork/ThisWeek/');    
		}
		else
		{
			$afterworks = AfterworkModel::GetWeek();
			foreach($afterworks as $a)
			{
				if($a->id_login == $login->id)
				{
					$error = true;
					$this->data['error'] = 'Vous ne pouvez pas proposer plus d\'un afterwork par semaine !';
					$this->View('Error.php');
					break;
				}
			}
			if (!isset($error))
				$this->View('Empty.php');
		}
	}
	
	public function ActionAddNextWeek($request)
	{
		$login = LoginModel::GetFromUsername($_SESSION['login']);
		if (count($request) > 0)
		{
			$afterwork = new AfterworkModel();
			foreach ($request as $key => $value) {
				if ($key == 'date')
				{
					$date = str_replace('/', '-', $value);
					$afterwork->$key = date('Y-m-d', strtotime($date));
				}
				else
					$afterwork->$key = $value;
			}
			
			$afterwork->id_login = $login->id;
			$afterwork->selected = 0;
			$afterwork->save();
			header('Location: /Afterwork/NextWeek/');    
		}
		else
		{
			$afterworks = AfterworkModel::GetNextWeek();
			foreach($afterworks as $a)
			{
				if($a->id_login == $login->id)
				{
					$error = true;
					$this->data['error'] = 'Vous ne pouvez pas proposer plus d\'un afterwork par semaine !';
					$this->View('Error.php');
					break;
				}
			}
			if (!isset($error))
				$this->View('Empty.php');
		}
	}
	
	public function ActionConfirm($request)
	{
		if (isset($request['af_id']))
		{
			$after_id = $request['af_id'];
			$login = LoginModel::GetFromUsername($_SESSION['login']);
			
			$subscription = new SubscriptionModel();				
			$subscription->afterwork_id = $after_id;
			$subscription->login_id = $login->id;
			
			$existingSubscriptions = SubscriptionModel::GetExistingAll($login->id);
			
			$existingVote = null;
			$existingAnswer = null;
			$gotoAnotherAfterwork = false;			
			
			foreach ($existingSubscriptions as $sub)
			{
				if ($sub->afterwork_id == $after_id)
				{
					$existingSubscription = $sub;
				}
				else
				{
					if($sub->answer == 'Yes')
						$gotoAnotherAfterwork = true;
				}				
			}
			
			if (isset($existingSubscription))
				$subscription = $existingSubscription;
			
			if (isset($request['Answer']))
			{			
				if ($gotoAnotherAfterwork && $request['Answer'] == 'Yes')
				{
					echo 'Vous avez déjà confirmé un autre afterwork !';
					return;
				}
					
				$subscription->answer = $request['Answer'];
				$subscription->save();
			}
			else if (isset($request['Vote']))
			{
				$subscription->vote = $request['Vote'];
				$subscription->save();
			}
		}
	}
}
?>
